package com.td.core.obj;

import java.util.ArrayList;
import java.util.List;

import com.td.core.communication.Sendable;
import com.td.core.messages.TDMessage;
import com.td.core.obj.bullet.Bullet;
import com.td.core.obj.logic.TowerLogic;
import com.td.core.obj.tower.Tower;
import com.td.core.obj.unit.Unit;

public class Player implements Sendable<Player> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected int gold;
	
	protected int points;
	
	protected int passedUnits;
	
	protected boolean gameOver;
	
	protected PlayerStats stats;
	
	protected List<Tower> towers;
	protected List<Unit> units;
	protected List<Bullet> bullets;
	
	protected List<TDMessage> messages;
	
	public Player() {
		this.gold = 100;
		this.points = 0;
		this.passedUnits = 0;
		this.gameOver = false;
		
		this.stats = new PlayerStats();
		
		towers = new ArrayList<Tower>();
		units = new ArrayList<Unit>();
		bullets = new ArrayList<Bullet>();
		messages = new ArrayList<TDMessage>();
		
	}

	public Player(Player pl) {
		this.gold = pl.gold;
		this.points = pl.points;
		this.passedUnits = pl.passedUnits;
		this.gameOver = pl.gameOver;
		
		this.stats = new PlayerStats(pl.getStats());
		
		this.towers = new ArrayList<Tower>();
		for(Tower tower: pl.towers)
			this.towers.add(TowerLogic.getRightTower(tower));
		
		this.units = new ArrayList<Unit>();
		for(Unit unit: pl.units)
			this.units.add(new Unit(unit));
				
		this.bullets = new ArrayList<Bullet>();
		for(Bullet bullet: pl.bullets)
			this.bullets.add(new Bullet(bullet));
				
		this.messages = new ArrayList<TDMessage>();
		this.messages.addAll(pl.messages);		
					
	}
	
	public boolean isGameOver() {
		return gameOver;
	}

	public void setGameOver(boolean gameOver) {
		this.gameOver = gameOver;
	}

	public int getPassedUnits() {
		return passedUnits;
	}

	public void passUnit() {
		this.passedUnits++;
	}

	public int getPoints() {
		return points;
	}
	
	public void addPoints(int points) {
		this.points += points;
	}
	
	public void setPoints(int points) {
		this.points = points;
	}

	public synchronized int getGold() {
		return gold;
	}

	public synchronized void setGold(int gold) {
		this.gold = gold;
	}
	
	public synchronized void subGold(int gold) {
		this.gold -= gold;
	}
	
	public synchronized void addGold(int gold) {
		this.gold += gold;
	}
	
	public synchronized List<Tower> getTowers() {
		return towers;
	}

	public synchronized List<Unit> getUnits() {
		return units;
	}
	
	public synchronized List<Bullet> getBullets() {
		return bullets;
	}
	
	
	public List<TDMessage> getMessages() {
		return messages;
	}
	
	public PlayerStats getStats() {
		return stats;
	}

	@Override
	public synchronized void copyTo(Player obj) {
		synchronized (obj) {
			obj.gold = this.gold;
			obj.points = this.points;
			obj.passedUnits = this.passedUnits;
			obj.gameOver = this.gameOver;
			
			this.stats.copyTo(obj.stats);
			
			obj.towers.clear();
			obj.towers.addAll(this.towers);
			
			obj.units.clear();
			obj.units.addAll(this.units);
					
			obj.bullets.clear();
			obj.bullets.addAll(this.bullets);	
			
			obj.messages.clear();
			obj.messages.addAll(this.messages);
		}
	}

	@Override
	public Player getCopy() {
		return new Player(this);
	}
}
